﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class LogRecordTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("source", "description", "text")]
        public void Can_CRUD(string source, string description, string text)
        {
            var record = Processor.Execute(new CreateLogRecord
            {
                Source = source,
                Description =  description,
                Text = text,
                Flags = LogRecordFlags.None
            }).Result;

            Assert.NotNull(record);
            record.Source.AreEqualIgnoringCase(source);
            record.Description.AreEqualIgnoringCase(description);
            record.Text.AreEqualIgnoringCase(text);
            record.Flags.AreEqual(LogRecordFlags.None);
            record.AreEqual(Processor.Execute(new GetLogRecord
            {
                EntityID = record.EntityID
            }).Result);

            source = source.Reverse();
            description = description.Reverse();
            text = text.Reverse();
            var upd_record = Processor.Execute(new UpdateLogRecord
            {
                EntityID = record.EntityID,
                Source = source,
                Description = description,
                Text = text,
                Flags = LogRecordFlags.Error
            }).Result;

            Assert.NotNull(upd_record);
            upd_record.AreEqual(record);
            upd_record.Source.AreEqualIgnoringCase(source);
            upd_record.Description.AreEqualIgnoringCase(description);
            upd_record.Text.AreEqualIgnoringCase(text);
            upd_record.Flags.AreEqual(LogRecordFlags.Error);

            Processor.Execute(new DeleteLogRecord
            {
                EntityID = record.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Source_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateLogRecord
            {
                Source = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Source_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateLogRecord
            {
                Source = String.Empty
            }));
        }

        [TestCase("source", "message")]
        public void Can_Create_From_Exc(string source, string message)
        {
            var ex = new Exception(message)
            {
                Source = source
            };
            var msg = Processor.Execute(new CreateLogRecordFromException
            {
                Ex = ex
            }).Result;

            Assert.NotNull(msg);
            msg.Source.AreEqualIgnoringCase(source);
            msg.Description.AreEqualIgnoringCase(message);
            msg.Text.AreEqualIgnoringCase(ex.ToString());
        }
    }
}
